package hesso.smartdraw.ch.model;

import java.util.ArrayList;
import java.util.Arrays;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class Level implements Parcelable{

	private int id = 0;
	private String name = "default";
	private int level = 0;
	private ArrayList<Room> rooms = new ArrayList<Room>();
	
	public Level(String name){
		this.name = name;
	}
	
	public Level(){
		this("unnamed Level");
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public ArrayList<Room> getRoomArray() {
		return rooms;
	}

	public void setRoomArray(ArrayList<Room> rooms) {
		this.rooms = rooms;
	}

	public void addRoom(Room room){
		this.rooms.add(room);
	}
	
	public void removeRoom(Room room){
		this.rooms.remove(room);
	}
	
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public void writeToParcel(Parcel dest, int flags) {
    	Log.d("tryout", "write level Parcel");
		dest.writeInt(id);
		dest.writeInt(level);
		dest.writeString(name);
		dest.writeParcelableArray(rooms.toArray(new Room[rooms.size()]), 0);
    	Log.d("tryout", "Level Parcel written");	
	}
	
	public static final Parcelable.Creator<Level> CREATOR = new Parcelable.Creator<Level>() {
		@Override
		public Level createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			return new Level(source);
		}

		@Override
		public Level[] newArray(int size) {
			// TODO Auto-generated method stub
			return new Level[size];
		}		
	};
	
	private Level(final Parcel in){
		readFromParcel(in);
	}
	
	public void readFromParcel(final Parcel in){
    	Log.d("tryout", "read from Level Parcel");
		id = in.readInt();
		level = in.readInt();
		name = in.readString();
		rooms = (ArrayList<Room>)Arrays.asList((Room[])in.readArray(ClassLoader.getSystemClassLoader()));
	}
}